package com.ysyx.ysyx_admin.controller;





import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.ysyx.ysyx_admin.config.KaptchaConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
@RequestMapping("/kaptcha")
public class KaptchController {


    private DefaultKaptcha kaptchaProducer = KaptchaConfig.getKaptchaBean();

    @RequestMapping("/getkaptcha")
    public void fileDownload(HttpSession session, HttpServletResponse response) {
        String text = kaptchaProducer.createText();
        session.setAttribute("code", text.toLowerCase());
        System.err.println(text.toLowerCase());
        BufferedImage image = kaptchaProducer.createImage(text);
        try {
            ImageIO.write(image, "jpg", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
